function Util(){
	this.baseUrl = 'http://api.shihou.tv/api/gift';
	this.receive_url = this.baseUrl + '/receive_rank?gift_id=';
	this.send_url = this.baseUrl + '/send_rank?gift_id=';
	//礼物id
	this.gift_id_array = ['1072', '1073', '1074', '1075'];
	//礼物名称和礼物id对应
	this.gift_name_array = ['冰块', '西瓜汁', '冰淇凌', '游泳圈'];
	//html榜单id,一定要按照顺序
	this.html_rank_id_array = [['phbList', 'phbList'], ['phbList', 'phbList'], ['phbList', 'phbList'], ['phbList', 'phbList']];
	//html个人排名id
	this.html_user_rank_id_array = [['phb_user_rank1', 'phb_user_rank2'], ['phb_user_rank1', 'phb_user_rank2'], ['phb_user_rank1', 'phb_user_rank2'], ['phb_user_rank1', 'phb_user_rank2']];
	//html个人排名描述
	this.html_user_rank_desc_array = [['冰块收礼排名：', '冰块贡献排名：'], ['西瓜收礼排名：', '西瓜贡献排名：'], ['冰淇淋收礼排名：', '冰淇淋贡献排名：'], ['泳圈收礼排名：', '泳圈贡献排名：']];
	
	//论坛相关
	this.pageSize = 10;
	this.pageNum = 1;
	this.auditStatus = 2;//1-审核中，2-审核通过
	this.enable = 1;//0-不启用，1-启用
	this.orderType = 1;//1-时间，2-点赞数量
	this.id = null;
	this.uid = null;	//当前用户id
	this.toUid = null;
	this.content = null;//发表评论的内容
	this.sentGift = false;//是否发送过礼物的状态， true发过，false没有发过
	
	this.self_config = new Config();
	this.init();
}
Util.prototype={
	init: function(){
	},
	setRankList: function(i, page){
		var _this = this;
		//拿到收礼榜url
		var receive_url = _this.receive_url + _this.gift_id_array[i];
		//拿到送礼榜url
		var send_url = _this.send_url + _this.gift_id_array[i];
		var receive_data_array = [];
		var send_data_array = [];
		var min = 0;
		var now = 0;
		//绑定榜单
		//收到
		_this.log("收到" + _this.gift_name_array[i]);
		_this.requestRemoteDataJson(receive_url, {}, function(result){
			if(_this.parseResult(result, 'error') == 0){
				var resultData = _this.parseResult(result, 'data', 'list');
				for (var i = 0;i < resultData.length; i++) {
					var user = resultData[i];
					receive_data_array[i] = user;
					now = i;
					if(i == 9){
						break;
					}
				}
			}
		}, false);
		min = now;
		_this.log("送出" + _this.gift_name_array[i]);
		_this.requestRemoteDataJson(send_url, {}, function(result){
			if(_this.parseResult(result, 'error') == 0){
				var resultData = _this.parseResult(result, 'data', 'list');
				for (var i = 0;i < resultData.length; i++) {
					var user = resultData[i];
					send_data_array[i] = user;
					now = i;
					if(i == 9){
						break;
					}
				}
			}
		}, false);
		if(min > now){
			min = now;	
		}
		
		var html_13 = '';
		var html_410 = '';
		//排行榜赋值
		for(var j = 0;j < min+1;j++){
			var receive_data = receive_data_array[j];
			var send_data = send_data_array[j];
			var roomSkip = '';
			if(page == 0){
				roomSkip = _this.parseResult(receive_data, 'user', 'room', 'jump');
			}else{
				roomSkip = 'http://event.m.shihou.tv/lion_module_share_temp/share?room_id='+_this.parseResult(receive_data, 'user', 'room', 'id');
				//roomSkip = 'http://m.shihou.tv/redirect_room?room_id='+_this.parseResult(receive_data, 'user', 'room', 'id');
			}
			if(j < 10){
				html_13 += _this.setRankListHTML((j+1), 1, roomSkip, _this.parseResult(receive_data, 'user', 'nick_name'), _this.parseResult(receive_data, 'user', 'avatar'), _this.parseResult(receive_data, 'user', 'room', 'vdo_status'), ''+_this.resetTotal(_this.parseResult(receive_data, 'total'))+'清凉值');
				html_13 += _this.setRankListHTML((j+1), 2, '', _this.parseResult(send_data, 'user', 'nick_name'), _this.parseResult(send_data, 'user', 'avatar'), _this.parseResult(send_data, 'user', 'room', 'vdo_status'), ''+_this.resetTotal(_this.parseResult(send_data, 'total'))+'清凉值');	
			}else{
				html_410 += _this.setRankListHTML((j+1), 1, roomSkip, _this.parseResult(receive_data, 'user', 'nick_name'), _this.parseResult(receive_data, 'user', 'avatar'), _this.parseResult(receive_data, 'user', 'room', 'vdo_status'), ''+_this.resetTotal(_this.parseResult(receive_data, 'total'))+'清凉值');
				html_410 += _this.setRankListHTML((j+1), 2, '', _this.parseResult(send_data, 'user', 'nick_name'), _this.parseResult(send_data, 'user', 'avatar'), _this.parseResult(send_data, 'user', 'room', 'vdo_status'), ''+_this.resetTotal(_this.parseResult(send_data, 'total'))+'清凉值');
			}
		}
		$('#'+_this.html_rank_id_array[i][0]).html(html_13);
		//$('#'+_this.html_rank_id_array[i][1]).html(html_410);
		
	},
	setUserRank: function(i, user_id){
		var _this = this;
		//拿到收礼榜url
		var receive_url = _this.receive_url + _this.gift_id_array[i];
		//拿到送礼榜url
		var send_url = _this.send_url + _this.gift_id_array[i];
		var user_receive_id = _this.html_user_rank_id_array[i][0];
		var user_send_id = _this.html_user_rank_id_array[i][1];
		var user_receive_desc = _this.html_user_rank_desc_array[i][0];
		var user_send_desc = _this.html_user_rank_desc_array[i][1];
		var receive_data = [];
		var send_data = [];
		//主播排名
		_this.requestRemoteDataJson(receive_url, {}, function(result){
			if(_this.parseResult(result, 'error') == 0){
				var resultData = _this.parseResult(result, 'data', 'list');
				for (var i = 0;i < resultData.length; i++) {
					var user = resultData[i];
					receive_data['"'+_this.parseResult(user, 'user', 'id')+'"'] = (i+1);
				}
				$('#' + user_receive_id).html(user_receive_desc + setUR(receive_data, user_id));		
			}
		}, true);
		//用户（玩家）排名
		_this.requestRemoteDataJson(send_url, {}, function(result){
			if(_this.parseResult(result, 'error') == 0){
				var resultData = _this.parseResult(result, 'data', 'list');
				for (var i = 0;i < resultData.length; i++) {
					var user = resultData[i];
					send_data['"'+_this.parseResult(user, 'user', 'id')+'"'] = (i+1);
				}
				$('#' + user_send_id).html(user_send_desc + setUR(send_data, user_id));		
			}
		}, true);
		
		function setUR(obj, user_id){
			var resu = obj['"'+user_id+'"'];
			if(resu == undefined){
				return '未上榜';
			}
			return resu;
		}
	},
	setRankListHTML: function(index, num, skipRoom, nick_name, avatar, kaibo_status, value){
		var html = '';
		var liClass = ''; //主播还是用户
		var aClass = 'liveNo'+index;//名次class
		var avatarHTML = '';
		var kaiboHTML = '';
		var rankIcon = '';
		
		if(index < 4){
			rankIcon = '<img src="static/img/icon'+index+'.png" />';
		}else{
			rankIcon = index;
			aClass = 'liveNo4';
		}
		
		if(kaibo_status == 1){//如果开播
			kaiboHTML = '    <em class="liveIcon"></em>'+
						'	 <em class="liveOpacity"></em>'+
                        '    <div class="loader-inner line-scale-pulse-out">'+
                        '        <div></div>'+
                        '        <div></div>'+
                        '        <div></div>'+
                        '        <div></div>'+
                        '        <div></div>'+
                        '    </div>';
		}
		if(num == 1){//第1排
			liClass = 'anchor';
			clas = 'font1';
		    avatarHTML = '<span class="liveHead">'+
                         '   	<span class="liveWrap">'+
                         			 kaiboHTML+
                         '           <img src="'+avatar+'"/>'+
                         '       </span>'+
                         '   </span>'+
                         '	<span class="liveName">'+nick_name+'</span>'+
                         '  <span class="liveNum">'+value+'</span>'+
                         '<span class="liveRanking">'+rankIcon+'</span>';    
            html = '<li class="'+liClass+'">'+
			   '	<a href='+skipRoom+' class="'+aClass+'">'+
	               	avatarHTML+
	           '	</a>'+    
	           '</li>';
		}else{//第2排
			liClass = 'user';
		    avatarHTML = '<span class="liveHead">'+
		    			 '	<img src="'+avatar+'"/></span>'+
                    	 '	<span class="liveName">'+nick_name+'</span>'+
                         '<span class="liveNum">'+value+'</span>';
            html = '<li class="'+liClass+'">'+
			   '	<a href="javascript:void();" class="'+aClass+'">'+
	               	avatarHTML+
	           '	</a>'+    
	           '</li>';             
		}
		
		return html;
	},
	resetTotal: function(total){
		return total;
		if(total == undefined)
			return 0;
		return parseInt(total/6);
	},
	//论坛列表
	getFormList: function(url){
		var _this = this;
		var data = {"pageSize":_this.pageSize, "pageNum":_this.pageNum, "auditStatus":_this.auditStatus, "enable":_this.enable, "orderType":_this.orderType, "from_user_id": _this.uid};
		_this.logParameter(url, data);
		_this.requestRemoteDataJson(url, data, function(res){
			if(res.errorCode == "0"){
				var list = res.result.list;
				var html = '';
				var item_user_ids = '';
				for (var i=0;i < list.length;i++) {
					var zanImg = 'zan';
					var item = list[i];
					var curr_uid = _this.parseResult(item, 'uid');
					var create_time = _this.parseResult(item, 'create_time');
					var create_time1 = create_time.split(' ')[0];
					var create_time1_array = create_time1.split('-');
					create_time1 = create_time1_array[1]+'-'+create_time1_array[2];
					var create_time2 = create_time.split(' ')[1];
					var create_time2_array = create_time2.split(':');
					create_time2 = create_time2_array[0]+':'+create_time2_array[1];
					create_time = create_time1+' '+create_time2;
					if(_this.parseResult(item, 'fabulousStatus'))
						zanImg = 'yizan';
					html += '<li data-id="'+util.parseResult(item, 'id')+'" data-uid="'+curr_uid+'">'+
		                    '    <p class="listTitle"><span>'+util.parseResult(item, 'content')+'</span><a data-id="'+util.parseResult(item, 'id')+'" data-uid="'+curr_uid+'" onclick="fabulous(this);"><img src="static/img/'+zanImg+'.png"/></a></p>'+
		                    '    <p class="listFont">'+
		                    '        <a id="img-'+curr_uid+'" data-create-time="'+create_time+'"></a>'+
		                    '        <span>积赞：<em>'+util.parseResult(item, 'fabulous_count')+'</em></span>'+
		                    '    </p>'+
		                    '</li>';
					item_user_ids += '&user_ids[]='+curr_uid;
				}
				if(_this.pageNum == 1)
					$('.list').html(html);
				else
					$('.list').append(html);
					
				//根据用户id取到用户信息并赋值到页面
				_this.requestRemoteDataJson(_this.self_config.user_batch+item_user_ids, {}, function(userResult){
					if(userResult.error == 0){
						var users = _this.parseResult(userResult, 'data', 'users');
						for(var i = 0;i < users.length;i++){
							var user = users[i];
							var user_id = _this.parseResult(user, 'id');
							var nick_name = _this.parseResult(user, 'nick_name');
							var avatar = _this.parseResult(user, 'avatar');
							var createTimeHtml = $('#img-'+user_id).attr('data-create-time'); 
							$('#img-'+user_id).html('<em><img src="'+avatar+'"/></em><i>'+nick_name+'</i>'+createTimeHtml);
						}
					}
				})
			}
			
		}, true);
	},
	getFormList2: function(url, callback){
		var _this = this;
		var data = {"pageSize":_this.pageSize, "pageNum":_this.pageNum, "auditStatus":_this.auditStatus, "enable":_this.enable, "orderType":_this.orderType, "from_user_id": _this.uid};
		_this.logParameter(url, data);
		_this.requestRemoteDataJson(url, data, callback, false);
	},
	//发表评论
	pulibshComment: function(httpUrl, checkGiftUrl){
		var _this = this;
		var content = $("#content").val();
		if(content.length < 1){
			$(".pop").html("请填写评论内容");
			$(".pop").fadeIn(200);
			setTimeout('$(".pop").hide();', 2000);
			return;
		}
		/*
		if(!_this.checkSendGift(checkGiftUrl)){
			$(".pop").html("送过礼物才能进行评论哦!");
			$(".pop").fadeIn(200);
			setTimeout('$(".pop").hide();', 1500);
			return;
		}*/
		if(!_this.sentGift){
			$(".pop").html("送过礼物才能进行评论哦!");
			$(".pop").fadeIn(200);
			setTimeout('$(".pop").hide();', 2000);
			return;
		}
		var data = {"content":_this.content, "uid": _this.uid};
		_this.requestRemotePostDataJson(httpUrl, data, function(res){
			if(res.errorCode != "0" ){
				if(res.errorCode == "305" ){
					$(".pop").html("请填写评论内容");
				}else{
					$(".pop").html(res.errorDescription);
				}
			}else{
				$(".pop").html('小师妹正在审核中~');
			}
			$(".pop").fadeIn(200);
			setTimeout('$(".pop").hide();$("#content").val("");', 1500);
		}, true);
	},
	checkSendGift: function(url){
		var _this = this;
		for (var i = 0;i < _this.gift_id_array.length;i ++) {
			var data = {"user_id":_this.uid, "gift_id": _this.gift_id_array[i]};
			_this.logParameter(url, data);
			_this.requestRemoteDataJson(url, data, function(res){
				_this.logResult(res);	
				if(res.error == 0 ){
					var sent = _this.parseResult(res, 'data', 'sent');
					if(sent){
						_this.sentGift = true;
						return true;
					}
				}
			}, true);
		}
		_this.sentGift = false;
		return false;
	},
	//点赞动作
	fabulous: function(url, obj){
		var _this = this;
		var data = {"id":_this.id, "uid": _this.uid, "toUid":_this.toUid};
		_this.requestRemotePostDataJson(url, data, function(data){
			_this.logResult(data);
			if(data.errorCode == "0" ){
				$(".pop").html("点赞成功");
				$(".pop").fadeIn(200);
				setTimeout('$(".pop").hide()', 3000);
				var target = $(obj).parent().next().find('span em');
				var fabulous = parseInt(target.html());
				target.html(fabulous+1);
				$(obj).find('img').attr('src', 'static/img/yizan.png');
			}else{
				$(".pop").html(data.errorDescription);
				$(".pop").fadeIn(200);
				setTimeout('$(".pop").hide()', 3000);
			}
		}, true);
	},
	//获取url中的参数
    getUrlParam: function(name, url) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        url = url || window.location.search.substr(1).match(reg);  //匹配目标参数
        if (url != null) return unescape(url[2]); return null; //返回参数值
    },
    getUrlParams: function(){
    	var url = window.location.href;  //匹配目标参数
    	if(url != null)
    		return url.substr(url.indexOf("?"), url.length);
    },
    parseResult:function(){//解析结果， 如果不存在则返回''
		var size = arguments.length;
		if(size == 0){
			return '';
		}
		var result = arguments[0];
		for(var i = 1; i < size; i++){
			if(result == undefined){
				return '';
			}
			var argu = arguments[i];
			if(i == size){
				return argu;
			}
			result = result[argu];
		}
		return result;
	},
	requestRemoteDataJson: function(url, data, callback, asyn){
		//this.logURL(url);
		//this.logParameter(data);
		$.ajax({
	        url: url,
	        type: 'GET',
	        async: asyn,
	        cache: false, 
	        dataType: "json",
	        data: data,
	        success: callback,
	        error: function (data) {
	        	var status = data.status;
	        	console.log(url + "获取数据失败!");
	        }
	    });
	},
	requestRemotePostDataJson: function(url, data, callback, asyn){
		//this.logURL(url);
		$.ajax({
	        url: url,
	        type: 'POST',
	        async: asyn,
	        cache: false, 
	        dataType: "json",
	        data: data,
	        success: callback,
	        error: function (data) {
	        	var status = data.status;
	        	console.log(url + "获取数据失败!");
	        }
	    });
	},
	log:function(data){
		console.log(data);
	},
	logURL:function(url){
		console.log("url --> "+url);
	},
	logParameter:function(data){
		console.log("parameter --> "+JSON.stringify(data));
	},
	logParameter:function(args, data){
		console.log(args+" --> "+JSON.stringify(data));
	},
	logResult:function(result){
		console.log("result --> "+JSON.stringify(result));
	}
}
